<?php
namespace App\Services;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use OSS\OssClient;
use OSS\Core\OssException;

class OssService
{
    private $accesskeyId = '';

    private $accesskeySecret = '';

    private $host = '';

    private $service;

    const DOMAIN = 'http://image.hanlingsha.love';

    const IMAGE_BUCKET = 'zhihuishu-image';

    public function __construct()
    {
        $this->accesskeyId = $_SERVER['OSS_ACCESS_ID'];
        $this->accesskeySecret = $_SERVER['OSS_ACCESS_SECRET'];
        $this->host = $_SERVER['OSS_HOST'];
        $this->service = new OssClient($this->accesskeyId, $this->accesskeySecret, $this->host);
    }

    public function upload($bucket, $file, $name)
    {
        try {
            $ret = $this->service->uploadFile($bucket, $name, $file);
            if ($ret == false) {
                return false;
            }
            $url = self::DOMAIN.$name;
            return $url;
        } catch (OssException $e) {
            Log::error($e->getMessage());
            return false;
        }
    }

    public function uploadImg($bucket, $file, $name)
    {
        $url = $this->upload($bucket, $file, $name);
        if (!$url) {
            return false;
        }
        $ch = curl_init();
        $tmp= substr(self::DOMAIN, -1);
        if ($tmp === '/') {
            $url = self::DOMAIN.$name;
        } else {
            $url = self::DOMAIN.'/'.$name;
        }
        curl_setopt($ch, CURLOPT_URL, $url.'?x-oss-process=image/info');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        if (false === $output) {
            return false;
        }
        return array('url'=>$url, 'info'=>json_decode($output, true));
    }

    public function uploadPic($file, $name, $dir='novel/cover')
    {
        $SMALL_LIMIT =  125;
        $ret = $this->uploadImg(
            self::IMAGE_BUCKET,
            $file,
            $this->getUploadName($dir, $name)
        );
        if (false === $ret) {
            return $ret;
        }
        $result = array();
        $height = $ret['info']['ImageHeight']['value'];
        $width = $ret['info']['ImageWidth']['value'];
        $normal = array('url' => $ret['url'], 'w' => $width, 'h' => $height);
        $result['n'] = $normal;
        if ($height > $SMALL_LIMIT) {
            $result['s'] = array(
                'url' => $ret['url'].'@'.$SMALL_LIMIT.'h',
                'h' => $SMALL_LIMIT,
                'w' => intval($SMALL_LIMIT * $width / $height)
            );
        } else {
            $result['s'] = $normal;
        }
        return $result;
    }

    private function getUploadName($type, $name)
    {
        mt_srand(); //optional for php 4.2.0 and up.

        $uuid = strtoupper(md5(uniqid(Str::random(), true)));

        return $type . '/' . date("Y-m-d") . '/' . $uuid . '.' . pathinfo($name, PATHINFO_EXTENSION);
    }
}
